/*
 * 项   目：spring-boot-demo
 * 作   者：LuoYiMin
 * 邮   箱：1725990142@qq.com
 * 创建时间：2018/9/21 16:15
 */
package cn.roin.learn.demo.controller;

import cn.roin.learn.demo.dto.Pageable;
import cn.roin.learn.demo.dto.ResultBean;
import cn.roin.learn.demo.entity.User;
import cn.roin.learn.demo.exception.BusinessException;
import cn.roin.learn.demo.service.UserService;
import cn.roin.learn.demo.dto.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * User-Controller
 */
@Controller
@RequestMapping("user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @RequestMapping("main.htm")
    public String toMain(){
        return "user/main";
    }
    @RequestMapping("add.htm")
    public String toAdd(){
        return "user/main";
    }
    @RequestMapping("update.htm")
    public String toUpdate(){
        return "user/main";
    }

    /**
     * 分页查询数据
     * @param pageable 查询参数封装类实体
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public PageInfo<User> list(Pageable pageable){
        PageInfo<User> page = userService.getPage(pageable);
        return page;
    }

    /**
     * 增加数据
     * @param user
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public ResultBean save(User user){
        ResultBean result = new ResultBean();
        try {
            if (user==null)
                result.setSuccess(false).setMsg("保存失败，保存信息为空！");
            else
                userService.save(user);
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("保存数据异常",e);
        }
        return result;
    }
    /**
     * 更新数据
     * @param user 用户实体，id属性不能为空
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultBean update(User user){
        ResultBean result = new ResultBean();
        try {
            if (user==null || StringUtils.isEmpty(user.getId())){
                result.setSuccess(false).setMsg("请选择要更新的数据！");
            }else
                userService.update(user);
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("更新数据异常",e);
        }
        return result;
    }

    /**
     * 单个删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultBean delete(@RequestParam(value = "id",required = true) String id){
        ResultBean result = new ResultBean();
        try {
            if (StringUtils.isEmpty(id)){
                result.setSuccess(false).setMsg("请选择要删除的数据！");
            }else
                userService.delete(id);
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("删除数据异常",e);
        }
        return result;
    }

    /**
     * 批量删除，最多支持1000个一次
     * @param ids
     * @return
     */
    @RequestMapping("delete_batch")
    @ResponseBody
    public ResultBean deleteBatch(@RequestParam(value = "ids",required = true) String[] ids){
        ResultBean result = new ResultBean();
        try {
            if (ids==null || ids.length==0){
                result.setSuccess(false).setMsg("请选择要删除的数据！");
            }else if(ids.length>1000){
                result.setSuccess(false).setMsg("一次最多删除500条记录！");
            }else{
                userService.deleteBatch(ids);
            }
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("批量删除数据异常",e);
        }
        return result;
    }
}
